#!/usr/bin/perl

require "./mailadmin-lib.pl";
&ReadParse();

my ($d, @data, @field, $dname, $err_msg, $k, $i, $len, $msg_over_batnum);
my ($match_style, $match_str, $sqlstr, $sqlcount, $wherestr, $orderstr, $limitstr);
my ($start, $count, $curpage, $tolpage, $curitem, $tolitems);
my (@vols, $mailroot);

unless (&qmail_dir_check())
{
    &redirect('maildir_edit.cgi');
}

$dname = $in{'dname'};
$match_style = $in{'match_style'};
$match_str = $in{'match_str'};
$msg_over_batnum = &text('msg_over_bataction_num', $config{'bataction_num'});
@vols = &get_volumes();
$mailroot = &qmail_dir_get();

unless ($dname)
{
    $err_msg = $text{'msg_lsuser_dname_empty'};
}

&header($text{'index_title'}, "", "list_user", 0, 1);

&foreign_require("mysql", "mysql-lib.pl");

$sqlcount = "select count(*)  from vpopmail ";
$sqlstr = "select pw_name, pw_domain, pw_shell, pw_clear_passwd  from vpopmail ";
$orderstr = " order by pw_name ";

unless ($match_style && $match_str) {
    $wherestr = " where pw_domain='$dname' ";
} else {
    $match_str = lc $match_str;
    
    $match_str =~ s/\'/\'\'/g;
    
    if ($match_style eq 'strictmatch') {
	$wherestr = " where pw_domain='$dname' and pw_name=\'$match_str\' ";
    }

    if ($match_style eq 'headmatch') {
	$wherestr = " where pw_domain='$dname' and pw_name like \'$match_str%\' ";
    }
    
    if ($match_style eq 'tailmatch') {
	$wherestr = " where pw_domain='$dname' and pw_name like \'%$match_str\' ";
    }

    if ($match_style eq 'submatch') {
	$wherestr = " where pw_domain='$dname' and pw_name like \'%$match_str%\' ";
    }
}

$d = &foreign_call("mysql", "execute_sql", 'vpopmail', 
		   $sqlcount . $wherestr);
$count = int($d->{'data'}->[0]->[0]);
$tolpage = int($count / $config{'perpage'}) + 1;

unless ($in{'jump'} > 0)
{
    $in{'jump'} = '1';
}

unless ($in{'jump'} <= $tolpage)
{
    $in{'jump'} = $tolpage;
}

if ($in{'jump'} > 0) {
    $in{'start'} = int($in{'jump'} - 1) *
	$config{'perpage'};
    if ($in{'start'} >= $count) {
	$in{'start'} = $count - $config{'perpage'};
	$in{'start'} = int(($in{'start'} / $config{'perpage'}) + 1) *
	    $config{'perpage'};
    }
}
else {
    $in{'start'} = int($in{'start'});
}

$tolitems = $count;
$curitem = $in{'start'} + 1;
$curpage = int($in{'start'} / $config{'perpage'}) + 1;

$limitstr = " limit " . $in{'start'} . "," . $config{'perpage'};

$d = &foreign_call("mysql", "execute_sql", 'vpopmail', 
		   $sqlstr . $wherestr . $orderstr . $limitstr);

@data = @{$d->{'data'}};

print "<form name=vuform action=list_user.cgi method=post onsubmit='return do_submit();'>\n";
#table total virturl user of domain
print "<table class=mode1 width=100% border=0>\n";

print "<tr><td valign=top width=90%>\n";

#list table of virturl user

print "<div style='valign:top;width:100%;height:300;overflow:auto'>";
print "<table class=mode1 width=100% celladding=0 cellspacing=0>\n";
print "<tr class=title1><td><b>$text{'th_uname'}</b></td><td><b>$text{'th_dname'}</b></td><td><b>$text{'th_mquota'}</b></td><td><b>$text{'th_clrpasswd'}</b></td><td width=15%><b>$text{'th_mbx_usage'}</b></td></tr>\n";
$i = 0;
my $cls;
my %uinfo;

for ($i = 0; $i < @data; $i++)
{
    @field = @{$data[$i]};
    if ($i % 2) {$cls = 'class=mode2'} else {$cls = 'class=mode1'};

    $b2m = $field[2];
    $b2m =~ s/\D//g;
    
#    $mpath = $field[3];
#    $mpath =~ s/^.*\/domains/domains/;

    &get_vuser_info($field[0], $field[1], \%uinfo);

    $clr_passwd = $field[3];
    
    printf "<tr $cls><td><input type=checkbox name=vuser value=\"$field[0]\" onclick=\"return chk_vuser(this);\">$field[0]</td><td>$field[1]</td><td>%.2f&nbsp;MB</td><td>$clr_passwd</td><td>$uinfo{'usage'}</td></tr>\n", $b2m/1048756;
}

$len = $i;

print "</table>";
print "</div>";
print "</td><td valign=top>";
print "<table class=mode1 border=0>";
print "<tr><td><input class=button type=submit name=btn_add value=\'$text{'btn_adduser'}\' onclick='return btn_adduser()'></td></tr>\n";
print "<tr><td><input class=button type=submit name=btn_mod value=\'$text{'btn_moduser'}\' onclick='return btn_moduser($len)'></td></tr>\n";
print "<tr><td><input class=button type=submit name=btn_del value=\'$text{'btn_deluser'}\' onclick='return btn_deluser($len)'></td></tr>\n";
#print "<tr><td><input class=button type=submit name=btn_sch value=\'$text{'btn_schuser'}\'></td></tr>\n";

print "<tr><td>";
print "<div style='valign:top;width:100%;height:50'>";
print "</div>";
print "</td></tr>";

print "<tr><td align=center><b>$text{'quick_search'}</b></td></tr>";
print "<tr><td align=center><select name=in_match_style class=list1>\n";
printf "<option value=\"strictmatch\" %s>$text{'strict_match'} \n", ($in{in_match_style} eq "strictmatch") ? "selected" : "";
printf "<option value=\"headmatch\" %s>$text{'head_match'} \n", ($in{in_match_style} eq "headmatch") ? "selected" : "";
printf "<option value=\"tailmatch\" %s>$text{'tail_match'} \n", ($in{in_match_style} eq "tailmatch") ? "selected" : "";
printf "<option value=\"submatch\" %s>$text{'sub_match'} \n", ($in{in_match_style} eq "submatch") ? "selected" : "";
print "</select></td></tr>";
#print "<tr><td><input name=match_style type=radio checked value=\"strictmatch\">$text{'strict_match'}</td></tr>";
#print "<tr><td><input name=match_style type=radio value=\"headmatch\">$text{'head_match'}</td></tr>";
#print "<tr><td><input name=match_style type=radio value=\"tailmatch\">$text{'tail_match'}</td></tr>";
#print "<tr><td><input name=match_style type=radio value=\"submatch\">$text{'sub_match'}</td></tr>";
print "<tr><td>$text{'uname_string'}<input name=in_match_str class=text size=15 value=\"$in{'in_match_str'}\"></td></tr>";
print "<tr><td align=center><input name=btn_sch type=submit class=button1 value=\"$text{'btn_schuser'}\" onclick='return btn_schuser();'></td></tr>";

print "</table>";
print "</td></tr>\n";
print "<tr ><td>\n";
print "<table width=100% class=mode1 border=0 celladding=0 cellspacing=0>";
print "<tr>\n";
printf "<td width=70>%s</td>", &text('allpage', $curpage, $tolpage);
printf "<td width=70>%s</td>", &text('allitems', $curitem, $tolitems);
printf "<td >%s</td>", &text('items', $len);
print "<td width=70 ><a href=list_user.cgi onclick='return do_prevpage();' >$text{'prevpage'}</a></td>";
print "<td width=70 ><a href=list_user.cgi onclick='return do_nextpage();' >$text{'nextpage'}</a></td>";
print "<td width=10><input class=button1 type=submit name=btn_goto value=\"$text{'gotopage'}\"></td>";
print "<td width=10><input class=text size=2 name=jump value=\"$in{'jump'}\"></td>";

print "<td width=10>$text{'page'}</td>";

print "</tr>";
print "</table>\n";

print "</td><tr>";
print "</table>";

print "<input type=hidden name=clk_btn value=\"\">";
print "<input type=hidden name=dname value=\"$dname\">";
print "<input type=hidden name=match_style value=\"$in{'match_style'}\">";
print "<input type=hidden name=match_str value=\"$in{'match_str'}\">";

print "</form>";


print <<EOF;
<script>
function do_prevpage()
{
    document.vuform.action = "list_user.cgi";
    document.vuform.jump.value--;
    document.vuform.submit();
    return false;
}

function do_nextpage()
{
    document.vuform.action = "list_user.cgi";
    document.vuform.jump.value++;
    document.vuform.submit();
    return false;
}

function do_submit()
{
    document.forms[0].btn_add.disabled = true;
    document.forms[0].btn_mod.disabled = true;
    document.forms[0].btn_del.disabled = true;
    document.forms[0].btn_sch.disabled = true;
    document.forms[0].btn_goto.disabled = true;
}

function chk_vuser(obj)
{
    var checkedlength, i;

    if(! obj.checked)
    {
	return true;
    }

    if (document.vuform.vuser.length > $config{'bataction_num'})
    {
	checkedlength = 0;
	for (i = 0; i < document.vuform.vuser.length; i++)
	{
	    if (checkedlength > $config{'bataction_num'})
	    {
		break;
	    }
	    if (document.vuform.vuser[i].checked)
	    {
		checkedlength++;
	    }
	}

	if (checkedlength > $config{'bataction_num'})
	{
	    alert("$msg_over_batnum");
     	    return false;
	}
    }
    return true;
}

function found_item(len)
{
    var found;

    if (len < 2)
    {
	if (len < 1)
	{
	    return false;
	}
	if (document.vuform.vuser.checked)
	{
	    return true;
	}
	else
	{
	    return false;
	}
    }

    for (i = 0; i < len; i++)
    {
	if (document.vuform.vuser[i].checked)
	{
	    return true;
	}
    }
    return false;
}

function btn_adduser()
{
    document.vuform.action = "create_vuser.cgi";
    document.vuform.clk_btn.value = "create";
}

function btn_moduser(len)
{
    document.vuform.action = "modify_vuser.cgi";
    document.vuform.clk_btn.value = "modify";

    if (len < 1)
    {
	alert("$text{'msg_no_select_item'}");
	return false;
    }

    if (found_item(len) == false)
    {
	alert("$text{'msg_no_select_item'}");
	return false;
    }
}

function btn_deluser(len)
{
    if (len < 1)
    {
	alert("$text{'msg_no_select_item'}");
	return false;
    }

    if (found_item(len) == false)
    {
	alert("$text{'msg_no_select_item'}");
	return false;
    }

    if(!confirm("$text{'msg_del_user'}"))
    {
	return false;
    }

    document.vuform.action = "delete_vuser.cgi";
    document.vuform.clk_btn.value = "delete";    

}

function btn_schuser()
{
    document.vuform.match_str.value = document.vuform.in_match_str.value;
    document.vuform.match_style.value = document.vuform.in_match_style.value;
    document.vuform.jump.value = "1";
    document.vuform.action = 'list_user.cgi';
}


</script>
EOF


&footer("list_domain.cgi", $text{"back_lsdomain"});
